package com.sbhotel.chotellauncher.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.constant.Constants;
import com.sbhotel.chotellauncher.util.HttpUtil;
import com.sbhotel.chotellauncher.util.SPUtil;
import com.sbhotel.chotellauncher.util.Utils;
import com.sbhotel.chotellauncher.widget.SettingsItemView;

public class SettingsActivity extends BaseActivity implements View.OnClickListener {

    View sleepTurnOff, sysInfo;
    TextView tvSleepValue;
    SettingsItemView groupView;

    @Override
    public void initView() {
        sleepTurnOff = findViewById(R.id.sleep_turn_off_layout);
        sysInfo = findViewById(R.id.sysinfo_layout);
        sleepTurnOff.setOnClickListener(this);
        sysInfo.setOnClickListener(this);

        tvSleepValue = findViewById(R.id.tv_sleep_value);
        groupView = findViewById(R.id.item_group);
        groupView.setOnClickListener(this);
    }

    @Override
    public void initData() {
        initTitleBar("个性设置", "");
        groupView.displayMoreIcon();
        groupView.setTitle("群组设置");
        groupView.setDesc("尚未设置群组关系");
        groupView.setIconImageId(R.mipmap.icon_group);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String sleepDefValue = (String) SPUtil.get(this, "sleep_value", "");
        if (!TextUtils.isEmpty(sleepDefValue)) {
            tvSleepValue.setText(sleepDefValue);
        } else {
            tvSleepValue.setText("关闭");
        }

        String groupName = (String) SPUtil.get(this, Constants.SP_KEY_GROUP_NAME, "");
        if (!TextUtils.isEmpty(groupName)) {
            groupView.setDesc(groupName);
            groupView.hideMoreIcon();
            groupView.setEnabled(false);
        }
    }

    @Override
    public int initLayout() {
        return R.layout.activity_settings;
    }

    @Override
    public void onClick(View view) {
        String reportId = "";
        Log.d(Constants.LOG_TAG, "click id = " + view.getId());
        switch (view.getId()) {
            case R.id.sleep_turn_off_layout:
                startActivityForResult(new Intent(SettingsActivity.this, SleepTurnOffActivity.class), 1);
                reportId = Constants.CODE_SETTINGS_SLEEP_MENU;
                break;
            case R.id.sysinfo_layout:
                startActivity(new Intent(SettingsActivity.this, SystemInfoActivity.class));
                reportId = Constants.CODE_SETTINGS_SYSINFO_MENU;
                break;
            case R.id.item_group:
                startActivityForResult(new Intent(SettingsActivity.this, GroupActivity.class), 2);
                break;
            default:
                break;
        }
        HttpUtil.dataReport(reportId, Utils.getDeviceUn(this));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_CENTER:
                //Toast.makeText(MainActivity.this, "点击了OK", Toast.LENGTH_SHORT).show();
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (sleepTurnOff.hasFocus()) {
                    startActivity(new Intent(SettingsActivity.this, SleepTurnOffActivity.class));
                } else if (sysInfo.hasFocus()) {
                    startActivity(new Intent(SettingsActivity.this, SystemInfoActivity.class));
                }
                break;
            case KeyEvent.KEYCODE_DPAD_UP:

                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 1:
                String result = data.getStringExtra("result");
                if (!TextUtils.isEmpty(result)) {
                    tvSleepValue.setText(result);
                }
                break;
            case 2:
                Toast.makeText(this, "已设置群组", Toast.LENGTH_SHORT).show();
                String groupName = data.getStringExtra("groupName");
                groupView.setDesc(groupName);
                break;
            default:
                break;
        }

    }
}
